const md5 = require('md5');
const Utils = {
  getRoutePath(vueComponent) {
    return vueComponent.$route.path;
  },

  getMethodName() {
    let result = null;
    let str = this.getStack();
    if (str) {
      let strArr = str.split('\n');
      if (strArr && strArr.length >= 4) {
        let destLine = strArr[3];
        result = destLine.match(/\.(\w+)\s*\(/)[1];
      }
    } else {
      result = arguments.callee.caller;
    }
    return result;
  },

  getStack() {
    try {
      let stackTrace = {};
      Error.captureStackTrace(stackTrace, this.getStack);
      return stackTrace.stack;
    } catch (e) {
      return null;
    }
  },

  judgePage(vueComponent) {
    let depth = 0;
    while (true) {
      try {
        vueComponent = vueComponent.$parent;
      } catch (e) {
        break;
      }
      depth++;
    }
    return depth == 3;
  },

  getIndex(vueComponent) {
    let name = vueComponent.$route.path || '';

    return md5(name);
  },

  getName(vueComponent) {
    let name = '';
    try {
      name = vueComponent.$route.path.replace(/^\//, '');
    } catch (error) {
      name = 'index';
    }
    return name;
  },

  filterChar(str) {
    // 过滤特殊字符
    let reg = /\n|\s|\t|\r/gi;
    return str ? str.replace(reg, '') : '';
  },

  addEvent(el, event, handler, capture = false) {
    if (el.addEventListener) {
      el.addEventListener(event, handler, capture);
    } else if (el.attachEvent) {
      el.attachEvent('on' + event, handler);
    } else {
      el['on' + event] = handler;
    }
  },

  generatorTime() {
    return new Date().getTime();
  },

  focus(el) {
    this.addEvent(el, 'focus', () => {
      //获取元素焦点时间
      this._inputStartTime = this.generatorTime();
    });
  },

  blur(el, vueComponent, options) {
    this.addEvent(el, 'blur', () => {
      // 获取元素焦点结束时间
      let innerText = vueComponent.elm.innerText;
      let elementName = this.filterChar(innerText);

      // 名称获取
      if (!/\S/gi.test(elementName)) {
        try {
          elementName = vueComponent.data.attrs['data-label'];
        } catch (error) {
          elementName = '';
        }
      }
      // 内容密码过滤
      let elementContent = '';
      if (/密码/.test(elementName)) {
        elementContent = el.value.replace(/./gi, '*') || '';
      } else {
        elementContent = el.value || '';
      }
      let event = {
        eventName: 'elementOper',
        pageIndex: this._pageIndex,
        pageName: this._pageName,
        elementName,
        collectType: '焦点事件采集',
        elementType: '输入框',
        inputType: '手工输入', //复制，手工输入
        elementContent: /密码/.test(elementName)
          ? el.value.replace(/./gi, '*') || ''
          : el.value || '',
        startTime: this._inputStartTime,
        endTime: this.generatorTime()
      };
      this.sendEvent(event, vueComponent, options);
    });
  },

  inputAction(el, vueComponent, options) {
    this.focus(el, vueComponent, options);
    this.blur(el, vueComponent, options);
  },

  sendEvent(event, vueComponent, options) {
    if (options && options.onEvent) {
      options.onEvent(event);
      return;
    }
    // 组装数据格式
    let EventName = event.eventName;
    delete event.eventName;
    let EventProperty = JSON.stringify(event);
    let params = { EventName, EventProperty };
    if (window.hasOwnProperty('AndroidJs')) {
      AndroidJs.htmlEventCollect(JSON.stringify(params));
    } else {
      console.log('==============================');
      console.log('no AndroidJs');
      console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
      console.log(event);
      console.log('==============================');
    }
  }
};

export default Utils;
